﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class gentelella_master_student_Default : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((db.proc_ISOpenDate(4)) < 1)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('该功能暂未开放，请等待！');window.location='../main.aspx'</script>");
            return;
        }
        PDF();
    }

    private void PDF()
    {
        #region 数据
        string phone = Session["phone"].ToString();
        var schoolRes = (from p in db.proc_GetBkgxNowYear()
                         select p).ToList().First();
        var m = (from p in db.proc_GetStuInfobyPwd(phone)
                 select p).ToList().First();
        string wbh = m.wbh;
        var result = (from p in db.proc_GetStuInfo()
                      where p.wbh == wbh
                      select p).ToList().First();
        var SubResult = (from p in db.proc_GetStuSubjectInfoByWbh(wbh)
                         select p).ToList();

        var classno = (from a in db.classAnn
                      where a.wbh == wbh
                      select a).ToList();

        string schoolInfo = schoolRes.schoolName + schoolRes.dYear + "年“专升本”考试";
        string zkzid = result.zkzh;
        string name = result.sname;
        string szyx = result.sdept;
        string bkzy = result.proName;
        string sfzid = result.sidfy;
        string zuohao = (classno[0].seatNo + 100).ToString().Substring(1, 2);
        string imgurl = Server.MapPath(@"~/StuImage/" + result.sidfy + ".jpg");
        string gkksid = result.gkbmh;
        #endregion

        #region pdf
        //字体 中文不显示解决
        //系统字体需要注意  引用iTextSharp库
        BaseFont bfchinese = BaseFont.CreateFont(@"c:\windows\fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font ChFont = new Font(bfchinese, 12);
        Font ChFont_black = new Font(bfchinese, 22, Font.BOLD, new BaseColor(0, 0, 0));
        Font ChFont_msg = new Font(bfchinese, 12, Font.ITALIC, BaseColor.RED);

        Font font_nr = new Font(bfchinese, 14, Font.NORMAL, new BaseColor(0, 0, 0));
        Font font_nr_b = new Font(bfchinese, 14, Font.BOLD, new BaseColor(0, 0, 0));
        Font font_snr = new Font(bfchinese, 8, Font.NORMAL, new BaseColor(0, 0, 0));
        float padding = 5f;

        string filepath = Server.MapPath(@"~/StuPdf/" + result.sidfy + ".pdf");
        Document doc = new Document(PageSize.A4.Rotate());
        PdfWriter pw = PdfWriter.GetInstance(doc, new FileStream(filepath, FileMode.Create));
        doc.Open();//开始

        PdfPTable ptable = new PdfPTable(3);//3 列
        PdfPCell cell;
        float[] width = { 0.8f, 3.6f, 1.6f };
        ptable.SetWidths(width);

        #region 0
        /*
        //1
        cell = new PdfPCell(new Phrase("\n", font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n", font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //3
        if (!File.Exists(barurl))
        {
            barurl = Server.MapPath(@"Pdf/stu/default.png");//图片不存在，显示默认
        }
        try
        {
            Image image = Image.GetInstance(barurl);
            cell = new PdfPCell(image, true);
            cell.Colspan = 1;
            cell.Border = 0;
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            ptable.AddCell(cell);
        }
        catch (Exception)
        {
            doc.Close();
            Response.Redirect("Error.aspx");
            return;
        }*/
        #endregion

        #region 1
        Font pdffont = new Font(bfchinese, 12, Font.NORMAL, new BaseColor(0, 0, 0));
        cell = new PdfPCell(new Phrase(schoolInfo, ChFont_black));
        cell.Colspan = 3;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        #endregion

        #region 2
        Font font_jkz = new Font(bfchinese, 16, Font.BOLD, new BaseColor(0, 0, 0));
        cell = new PdfPCell(new Phrase("\n准考证", ChFont_black));
        cell.Colspan = 3;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        #endregion

        #region 3
        //1
        cell = new PdfPCell(new Phrase("\n准考证号：", font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + zkzid, font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //3
        if (!File.Exists(imgurl))
        {
            imgurl = Server.MapPath(@"~/Hbpu.jpg");
        }
        try
        {
            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imgurl);
            cell = new PdfPCell(image, true);
            cell.Colspan = 1;
            cell.Border = 0;
            cell.Rowspan = 6;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            ptable.AddCell(cell);
        }
        catch (Exception)
        {
            //doc.Close();
            Response.Redirect("Error.aspx");
            return;
        }
        #endregion

        #region 4
        //1
        cell = new PdfPCell(new Phrase("\n姓    名：", font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + name, font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion

        #region 5
        //1
        cell = new PdfPCell(new Phrase("\n所在院校：", font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + szyx, font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion

        #region 6
        //1
        cell = new PdfPCell(new Phrase("\n报考专业：", font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + bkzy, font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion

        #region 7
        //1
        cell = new PdfPCell(new Phrase("\n身份证号：", font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + sfzid, font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion

        #region 8
        //1
        cell = new PdfPCell(new Phrase("\n座    号：", font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + zuohao, font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion

        #region 9
        //1
        cell = new PdfPCell(new Phrase("\n考试信息：", font_nr_b));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.PaddingBottom = padding;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n高考考生号：" + gkksid, font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.PaddingBottom = padding;
        cell.HorizontalAlignment = Element.ALIGN_RIGHT;
        ptable.AddCell(cell);
        //3
        cell = new PdfPCell(new Phrase("\n", font_nr));
        cell.Colspan = 1;
        cell.Border = 0;
        cell.PaddingBottom = padding;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion

        doc.Add(ptable);//第一个结束
        ptable = new PdfPTable(3);//第二个开始

        #region 10
        //1
        cell = new PdfPCell(new Phrase("考试科目", font_nr));
        cell.Colspan = 1;
        cell.PaddingBottom = padding;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("考试时间", font_nr));
        cell.Colspan = 1;
        cell.PaddingBottom = padding;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //3
        cell = new PdfPCell(new Phrase("考试地点", font_nr));
        cell.Colspan = 1;
        cell.PaddingBottom = padding;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion
        for (int i = 0; i < SubResult.Count; i++)
        {
            #region 
            //1
            cell = new PdfPCell(new Phrase(SubResult[i].subName.ToString(), font_nr));
            cell.Colspan = 1;
            cell.PaddingBottom = padding;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            ptable.AddCell(cell);
            //2
            cell = new PdfPCell(new Phrase(SubResult[i].testTime.ToString(), font_nr));
            cell.Colspan = 1;
            cell.PaddingBottom = padding;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            ptable.AddCell(cell);
            //3
            cell = new PdfPCell(new Phrase(SubResult[i].place.ToString(), font_nr));
            cell.Colspan = 1;
            cell.PaddingBottom = padding;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            ptable.AddCell(cell);
            #endregion
        }

        //空行
        #region
        cell = new PdfPCell(new Phrase("\n", font_snr));
        cell.Colspan = 3;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion

        //末尾
        #region 
        cell = new PdfPCell(new Phrase("考试须知： \n1.凭本人身份证、专升本准考证参加考试，证件不全者不得参加考试。", font_nr));
        cell.Colspan = 3;
        cell.PaddingBottom = padding;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion

        doc.Add(ptable);
        doc.Close();//结束
        #endregion

        Response.Redirect(@"~/StuPdf/" + result.sidfy + ".pdf");
    }
}